<script setup>
import { ref } from 'vue'
//先从api里面导入方法
import { artGetChannelsService } from '@/api/article'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'
//定义获取的数据变量，根据接口文档可知为一个对象数组
const channelList = ref([])
//定义方法
const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  channelList.value = res.data.data
  loading.value = false
}

const onEditChannel = (row) => {
  dialog.value.open(row)
  //row是对象数组，为整个表的数据，可用下标行序号找到对应行的数据$index是当前行的信息
}
const onDelChannel = (row, $index) => {
  console.log(row, $index)
}
const onAddChannel = () => {
  dialog.value.open({})
}

const loading = ref(false) //加载效果
getChannelList()

//编辑
const dialog = ref()
</script>
<template>
  <PageContainer title="文章标题">
    <template #extra>
      <el-button @click="onAddChannel">添加分类</el-button>
    </template>

    <el-table width="180" :data="channelList" v-loading="loading">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column prop="" label="操作">
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            @click="onEditChannel(row)"
            circle
            plain
            type="primary"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDelChannel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <template #empty>
      <el-empty description="没有数据"></el-empty>
    </template>

    <ChannelEdit ref="dialog"></ChannelEdit>
  </PageContainer>
</template>
